package com.web.kftsdk.common;

import com.web.kftsdk.common.bean.MultiSelect;
import com.web.kftsdk.common.util.BeanUtil;
import com.web.kftsdk.common.util.BlankUtil;
import com.web.kftsdk.common.util.str.StringUtils;
import com.google.common.collect.Lists;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * <p>Title: MultiSelectValidator</p>
 * <p>Description: TODO</p>
 *
 * @author xukk
 * @version 1.0
 * @date 2018/6/14
 */
public class MultiSelectValidator implements ConstraintValidator<MultiSelect,Object> {
    private List<String> multi;
    private Class classType;
    @Override
    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
        List<Object> values=new ArrayList<>();
        multi.forEach(v->{
            try {
                Optional.ofNullable(BeanUtil.getFieldValue(o,v)).ifPresent(t->values.add(t));
            } catch (Exception e) {
                values.add("");
            }
        });
        String template="选一";
        if(classType.getSimpleName().equalsIgnoreCase(String.class.getSimpleName())){
            if(StringUtils.isAnyNotBlank(values.stream().toArray(String[]::new))){
                return true;
            }else {
                constraintValidatorContext.disableDefaultConstraintViolation();
                constraintValidatorContext.buildConstraintViolationWithTemplate(String.format("%s"+template,trans(multi.size()))).addPropertyNode(multi.toString()).addConstraintViolation();
            }
        }else {
           if(values.stream().filter(v-> !BlankUtil.isBlank(v)).findAny().isPresent()){
               return true;
           }else {
               constraintValidatorContext.disableDefaultConstraintViolation();
               constraintValidatorContext.buildConstraintViolationWithTemplate(String.format("%s"+template,trans(multi.size()))).addPropertyNode(multi.toString()).addConstraintViolation();
           }
        }
        return false;
    }

    @Override
    public void initialize(MultiSelect constraintAnnotation) {
        this.multi= Lists.newArrayList(constraintAnnotation.multi());
        this.classType=constraintAnnotation.classType();
    }
    private static String trans(int num) {
        String[] numeric = new String[]{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
        StringBuilder builder = new StringBuilder();
        builder.append(numeric[num / 1000] + "千").
                append(numeric[num / 100 % 10] + "百").
                append(numeric[num / 10 % 10] + "十").
                append(numeric[num % 10]);
        //去掉了零千....
        int index = -1;
        while ((index = builder.indexOf(numeric[0], index + 1)) != -1) {
            if (index < builder.length() - 1) {
                builder.deleteCharAt(index + 1);
            }
        }
        //去掉双零
        index = 0;
        while ((index = builder.indexOf("零零", index)) != -1) {
            builder.deleteCharAt(index);
        }

        if (builder.length() > 1) {
            //去掉开头的零
            if (builder.indexOf(numeric[0]) == 0) {
                builder.deleteCharAt(0);
            }
            //去掉末尾的零
            if (builder.indexOf(numeric[0]) == builder.length() - 1) {
                builder.deleteCharAt(builder.length() - 1);
            }

        }
        //把开头一十换成十
        if (builder.indexOf("一十") == 0) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}
